package com.bee.tlweb.view;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.ext.servlet.ServletGroupTemplate;

import com.bee.tlweb.model.Email;
import com.bee.tlweb.model.User;
import com.bee.tlweb.service.EmailService;
import com.bee.tlweb.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String type = request.getParameter("type");
		if (type == null || type.length() == 0) {
			String name = request.getParameter("name");
			name = new String(name.getBytes("ISO8859-1"),"UTF-8");
			String password = request.getParameter("password");
			UserService service = new UserService();
			User user = service.login(name, password);
			EmailService emailService = new EmailService();
			List<Email> allMails = emailService.getLatestEmail(user.getId());
			request.getSession().setAttribute("user", user);
			request.setAttribute("allMails", allMails);

			ServletGroupTemplate.instance().render("/email/emailReceive.html", request, response);

			
			
		} else {
			// 退出
			request.getSession().removeAttribute("user");
			ServletGroupTemplate.instance().render("/index.html", request, response);

		}

	}

}
